package com.example.tianwu.daydayup;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends Activity {

    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
    private static final int PHOTO_ACTIVITY_REQUESR_CODE = 200;

    private ImageView lineChart;
    private ImageView paneChart;
    int lastBeautyValue = 0;
    int lastSmallValue = 0;

    private File photoFile;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent startShow = new Intent(this,StartShowActivity.class);
        startActivity(startShow);

        lineChart = (ImageView)findViewById(R.id.lineChart);
        paneChart = (ImageView)findViewById(R.id.paneChart);
        //启动时显示统计趋势图表
        float[] dataLine = SaveData.getLineData(this);
        float[] dataPane = SaveData.getPaneData(this);
        if(dataLine.length>1) {
            lastBeautyValue = (int) dataLine[(int) dataLine[0]];
            lastSmallValue = (int) dataPane[(int) dataPane[0]];
        }
        DrawChart.drawLineChart(lineChart, dataLine);
        DrawChart.drawPaneChart(paneChart, dataPane);

    }
    public void takePhoto(View view){

        photoFile = SaveData.getOutputMediaFile(SaveData.MEDIA_TYPE_IMAGE);
        Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        camera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
        startActivityForResult(camera, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

    }
    public void showHistory(View view){
        Intent history = new Intent(this,HistoryActivity.class);
        startActivity(history);
    }
    public void YoutuFinish(FaceData fd){//获取到分析数据后调用的方法
        //
        Toast.makeText(this,fd.toString(),Toast.LENGTH_LONG).show();
        String nowDate = new SimpleDateFormat("yyyyMMdd_HH").format(new Date());
        String photoId = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        SaveData.savePhotos(this,new Photo(photoId, photoFile.toString(),fd,nowDate));
        ((TextView)findViewById(R.id.curState)).setText(R.string.face_data);
        ((TextView)findViewById(R.id.beautyText)).setText(R.string.beauty_text);
        ((TextView)findViewById(R.id.expessionText)).setText(R.string.expession_text);
        ((TextView)findViewById(R.id.beautyValue)).setText(Integer.toString(fd.beauty));
        ((TextView)findViewById(R.id.smallValue)).setText(Integer.toString(fd.expression));
        if(fd.expression>=lastSmallValue){
            ((ImageView)findViewById(R.id.imageExpession)).setImageResource(R.drawable.value_up);
        }else {
            ((ImageView)findViewById(R.id.imageExpession)).setImageResource(R.drawable.value_down);
        }
        if(fd.beauty>=lastBeautyValue){
            ((ImageView)findViewById(R.id.imageBeauty)).setImageResource(R.drawable.value_up);
        }else {
            ((ImageView)findViewById(R.id.imageBeauty)).setImageResource(R.drawable.value_down);
        }
    }
    public void YoutuError(){
        Toast.makeText(this,"Failed!!!",Toast.LENGTH_LONG).show();
        String nowDate = new SimpleDateFormat("yyyyMMdd_HH").format(new Date());
        String photoId = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        SaveData.savePhotos(this,new Photo(photoId, photoFile.toString(),new FaceData(),nowDate));
        ((TextView)findViewById(R.id.curState)).setText(R.string.face_data);
        ((TextView)findViewById(R.id.beautyText)).setText(R.string.beauty_text);
        ((TextView)findViewById(R.id.expessionText)).setText(R.string.expession_text);
        ((TextView)findViewById(R.id.beautyValue)).setText("0");
        ((TextView)findViewById(R.id.smallValue)).setText("0");
        ((ImageView)findViewById(R.id.imageExpession)).setImageResource(R.drawable.value_down);
        ((ImageView)findViewById(R.id.imageBeauty)).setImageResource(R.drawable.value_down);

    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent returnIntent) {
        if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE ){//拍照后返回
            if(resultCode == RESULT_OK){//拍照成功调用预览页面
                Intent photo = new Intent(this,PhotoActivity.class);
                photo.putExtra("PhotoFile", photoFile.toString());
                startActivityForResult(photo,PHOTO_ACTIVITY_REQUESR_CODE);

                Toast.makeText(this,"Take a Photo.",Toast.LENGTH_LONG).show();
            }
        }
        if(requestCode == PHOTO_ACTIVITY_REQUESR_CODE){//预览返回
            if(resultCode == RESULT_OK){//预览确认
                ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
                if (networkInfo != null && networkInfo.isConnected()) {
                    TextView curState = (TextView)findViewById(R.id.curState);
                    curState.setText(R.string.get_face_data);
                    try {
                        //传输时对图片进行压缩处理
                        ByteArrayOutputStream tempOS = new ByteArrayOutputStream();
                        BitmapFactory.decodeFile(photoFile.toString()).compress(Bitmap.CompressFormat.JPEG, 80/*压缩质量0~100*/, tempOS);
                        tempOS.flush();
                        tempOS.close();
                        byte[] bitmapBytes = tempOS.toByteArray();
                        //网络连接线程
                        new PicYoutuData(this).execute(BitmapFactory.decodeByteArray(bitmapBytes,0,bitmapBytes.length));
                        /*
                        测试压缩后图片的质量损失
                        */
                    }catch (IOException ie){
                        Toast.makeText(this,ie.getMessage(),Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(this,"Network Error.",Toast.LENGTH_LONG).show();
                    // display error
                }
            }
            if(resultCode == RESULT_CANCELED){//预览重拍
                //重新启动相机拍摄
                Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                camera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                startActivityForResult(camera, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
            }
        }
    }



}
